<template>
  <div class="contract-view">
    <el-card class="contract-card">
      <div class="contract-content" ref="contractContent">
        <h1 class="title">租房无忧有限公司电子租赁合同</h1>
        <p class="contract-id">合同编号：{{ contractId }}</p>

        <p>甲方（出租人）：{{ landlordName }}</p>
        <p>乙方（承租人）：{{ tenantName }}</p>
        <p>
          根据《中华人民共和国合同法》及相关法律法规，甲乙双方在平等、自愿的基础上，就下列房屋的租赁事宜达成如下协议：
        </p>

        <div class="section">
          <h3>一、房屋信息</h3>
          <p>房源名称：{{ houseName }}</p>
        </div>

        <div class="section">
          <h3>二、租赁期限</h3>
          <p>自 {{ startDate }} 起，至 {{ endDate }} 止。</p>
        </div>

        <div class="section">
          <h3>三、租金及支付方式</h3>
          <p>总租金金额：¥{{ totalAmount }} 元</p>
          <p>支付方式：{{ paymentMethod }}</p>
        </div>

        <div class="section">
          <h3>四、双方约定</h3>
          <p>1. 乙方应按时支付租金，不得拖欠。</p>
          <p>2. 未经甲方书面同意，乙方不得擅自转租。</p>
          <p>3. 租赁期间，乙方应合理使用房屋设施，如有损坏应赔偿。</p>
        </div>

        <div class="footer">
          <div class="sign-line">
            <span>甲方签字：___________</span>
            <span>乙方签字：___________</span>
          </div>
          <div class="date">签署日期：{{ rentDate }}</div>
        </div>
      </div>

      <div class="btn-group">
        <el-button type="primary" @click="downloadContract">保存合同</el-button>
        <el-button @click="goBack">返回主页面</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import html2pdf from 'html2pdf.js'

const router = useRouter()
const route = useRoute()

const contractId = route.query.contractId || 'N/A'
const tenantName = route.query.tenantName || '租客姓名'
const landlordName = route.query.landlordName || '房东姓名'
const houseName = route.query.houseName || '房源名称'
const startDate = route.query.startDate || ''
const endDate = route.query.endDate || ''
const rentDate = route.query.rentDate || ''
const totalAmount = route.query.amount || '0'
const paymentMethod = route.query.payType || '未知方式'

const contractContent = ref(null)

const downloadContract = () => {
  const element = contractContent.value

  const opt = {
    margin:       0.5,
    filename:     `租房合同_${contractId}.pdf`,
    image:        { type: 'jpeg', quality: 0.98 },
    html2canvas:  { scale: 2 },
    jsPDF:        { unit: 'in', format: 'letter', orientation: 'portrait' }
  }

  html2pdf().set(opt).from(element).save()
}

const goBack = () => {
  router.push('/user')
}
</script>

<style scoped>
.contract-view {
  max-width: 850px;
  margin: 50px auto;
  padding: 20px;
}

.contract-card {
  padding: 40px;
  background: linear-gradient(to bottom, #fcfcfc, #f2f2f2);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.08);
  border-radius: 12px;
}

.contract-content {
  background-color: #fffdf8;
  padding: 30px;
  border-radius: 8px;
  font-family: "Songti SC", serif;
  font-size: 16px;
  line-height: 2;
  color: #333;
  border: 1px solid #dcdcdc;
}

.contract-content .title {
  font-size: 24px;
  text-align: center;
  font-weight: bold;
  margin-bottom: 20px;
}

.contract-content .contract-id {
  text-align: right;
  margin-bottom: 20px;
  font-weight: 500;
}

.section {
  margin-top: 25px;
}

.section h3 {
  font-size: 18px;
  font-weight: bold;
  color: #222;
  margin-bottom: 10px;
}

.footer {
  margin-top: 40px;
}

.sign-line {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
  margin-bottom: 20px;
}

.date {
  text-align: right;
  font-style: italic;
}

.btn-group {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  gap: 20px;
}
</style>
